<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/8/24
 * Time: 14:28
 */

namespace ticket\libs\Zhiyoubao;



use ticket\libs\Curl;
use ticket\libs\Tools;
use ticket\modules\frontend\models\ErrorMsg;
use ticket\modules\frontend\models\OrderDetails;
use ticket\modules\frontend\models\OrderInfo;
use ticket\modules\frontend\models\TicketInfo;
use ticket\modules\frontend\models\TicketLog;

class Zhiyoubao extends Configura
{

//    private $url = "http://boss.zhiyoubao.com/boss/service/code.htm";


    public function __construct($aid)
    {
        $this->getApplication($aid);
    }




    /*** 门票下单
     * @param string $aid
     * @param string $params
     * @return bool|mixed
     */
    public function getPlaceOrder($params = '')
    {
        $data = $this->getPlaceParams($params);
        $xml = Curl::post($this->place_url,$data);
        $res = Tools::FromXml($xml);
        if ($res['code'] == 0){
            ErrorMsg::setInfo($_SERVER['REQUEST_URI'],00000,'智游宝下单成功'.$this->place_url.json_encode($data,JSON_UNESCAPED_UNICODE),$res);
            $res['order_sn'] = $params['order_sn'];
            if (!empty($res['orderResponse']['order']['orderCode'])){
                $tickLogModel = new TicketLog();
                $tickLogModel->addInfo($res,$params);   // 记录下单日志信息
            }
            $verificationCode = $this->getQrcodeImgParams($params['order_sn']);
            if ($verificationCode){
                $model = OrderDetails::find()->where('id = :id',[':id' => $params['orderDetailsId']])->one();
                if (!is_null($model)){
                    $model->assist_check_no = $res['orderResponse']['order']['assistCheckNo'];
                    $model->verification_code = $verificationCode;
                    if ($model->save(0)){
                        $res = $this->sendmessages($params['order_sn']);
                        return true;
                    }
                }
            }else{
                return true;
            }
        }else{
            ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'智游宝下单失败'.$this->place_url.json_encode($data,JSON_UNESCAPED_UNICODE),$res);
            return false;
        }

    }


    /*** 获取门票核销码
     * @param $aid
     * @param $orderSn
     * @return false|string
     */
    public function getQrcodeImgParams($orderSn)
    {
        $params = $this->getQrcodeImg($orderSn);
        $result = Curl::post($this->place_url,$params);
        $state = Tools::FromXml($result);     // 把$result 转成数组
        if ($state['code'] == 0){      // 保存图片到本地
            $imageContent = base64_decode($state['img']);
            $dateUrl = "/uploads/qrcodeimg/";
            $date = '../../ticket/web'.$dateUrl;
            $fileUrl = time(). '.png';
            $filename = $date . $fileUrl;
            if(!file_exists($date)){//创建今天日期的文件夹
                mkdir($date, 0777, true);
                chmod($date, 0777);
            }
            $fp2 = @fopen($filename, "a");
            fwrite($fp2, $imageContent);//向当前目录写入图片文件，并重新命名
            fclose($fp2);
            $verificationCode = \Yii::$app->params['SITE_URL'].$dateUrl.$fileUrl;
            return $verificationCode;
        }else{
            ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'获取核销码失败'.json_encode($params,JSON_UNESCAPED_UNICODE),$state);
            $verificationCode = false;
        }
        return $verificationCode;
    }


    /*** 购票成功，发送短信给用户
     * @param $aid
     * @param $orderSn
     * @return bool|false|string
     */
    public function sendmessages($orderSn)
    {
        $params = $this->sendMessage($orderSn);
        $result = Curl::post($this->place_url,$params);
        $res = Tools::FromXml($result);     // 把$result 转成数组
        if ($res['code'] == 0){
            return true;
        }
        ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'发送短信失败'.json_encode($params,JSON_UNESCAPED_UNICODE),$result);
        return false;
    }


    /*** 退票
     * @param $params
     * @param string $refundNum
     * @return bool
     */
    public function refundTicket($params,$refundNum = '')
    {
        if ($refundNum == ''){
            $refundNum = 'RF'.Tools::createSn();
        }
        $paramsData = $this->getReturnTicket($params['pay_number'],$params['goods_num'],$refundNum,$params['certificate']);
        $result = Curl::post($this->place_url,$paramsData);
        $res = Tools::FromXml($result);     // 把$result 转成数组
        if ($res['code'] == 0){
            ErrorMsg::setInfo($_SERVER['REQUEST_URI'],000000,'退票成功'.json_encode($params,JSON_UNESCAPED_UNICODE),$result);
            return true;
        }
        ErrorMsg::setInfo($_SERVER['REQUEST_URI'],99999,'退票失败'.json_encode($params,JSON_UNESCAPED_UNICODE),$result);
        return false;

    }



}